package com.base.cn.platform.os.controller.course.classes;

import com.base.cn.platform.os.common.constants.CommonConstants;
import com.base.cn.platform.os.common.md5.MD5;
import com.base.cn.platform.os.common.utils.result.ResultUtil;
import com.base.cn.platform.os.controller.cmmon.ApiBaseController;
import com.base.cn.platform.os.service.course.classes.ClassesService;
import com.base.cn.platform.os.service.course.classes.member.ClassesMemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 班级Api接口
 *
 * @author s.li
 * @create 2018-09-17-9:31
 */
@RestController
@RequestMapping("/api/classes")
public class ApiClassesController extends ApiBaseController {

    @Autowired
    private ClassesService classesService;
    @Autowired
    private ClassesMemberService classesMemberService;
    @Autowired
    private CommonConstants commonConstants;

    /**
     * 获取全部下级列表
     * @param currentPage 页码
     * @param className 班级名（用于搜索）
     * @return Map<String,Object>
     */
    @RequestMapping("/getClassesList")
    public Map<String,Object> getClassesList(@RequestParam("currentPage") int currentPage,
                                             @RequestParam(value = "className",required = false) String className){
        Map<String,Object> clazz = new HashMap<>();
        clazz.put("status",1);
        clazz.put("name",className);
        Map<String,Object> dataMap = classesService.findClassesListPage(clazz, currentPage, 10, false, false, false, false, 1);
        return ResultUtil.SUCCESS(dataMap);
    }

    /**
     * 获取班级详情
     * @param classId 班级ID
     * @param userId 用户ID
     * @return Map<String,Object>
     */
    @RequestMapping("/info")
    public Map<String,Object> info(@RequestParam("classId")BigDecimal classId,
                                   @RequestParam(value = "userId",required = false,defaultValue = "0") BigDecimal userId){
        Map<String, Object> classes = classesService.findClassesById(classId, false, false, false, true, 0,3);
        //设置是否加入班级
        boolean joined = classesMemberService.joined(classId, userId);
        //设置班级成员信息
        Map<String, Object> condition = Collections.singletonMap("classesId", classId);
        List<Map<String, Object>> memberList = classesMemberService.findClassesMemberList(condition, true, 1,  2, 3);
        Map<String,Object> dataMap = new HashMap<>();
        dataMap.put("shareUrl",commonConstants.get(CommonConstants.MOBILE_PATH)+"/mobile/class/info?classId="+classId+"&shareKey="+MD5.getMD5(userId.toString())+"_"+userId);
        dataMap.put("classes",classes);
        dataMap.put("joined",joined);
        dataMap.put("memberList",memberList);
        return ResultUtil.SUCCESS(dataMap);
    }

    /**
     * 加入班级
     * @param userId 用户ID
     * @param classId 班级ID
     * @return Map<String,Object>
     */
    @RequestMapping("/joinClasses")
    public Map<String,Object> joinClasses(@RequestParam("userId") BigDecimal userId,
                                          @RequestParam("classId") BigDecimal classId){
        return classesMemberService.saveClassesMemberBatch(classId, userId.toString(), 1, 1);
    }


    /**
     * 获取关联班级
     * @param courseId 课程ID
     * @return Map<String,Object>
     */
    @RequestMapping("/getCourseRelevanceClasses")
    @ResponseBody
    public Map<String,Object> getCourseRelevanceClasses(@RequestParam( value =  "courseId") BigDecimal courseId){

        List<Map> courseRelevanceClasses = classesService.getCourseRelevanceClasses(courseId);
        return ResultUtil.SUCCESS(courseRelevanceClasses);
    }
}
